package com.bnu.graduate.Service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bnu.graduate.Entity.Graduate;
import com.bnu.graduate.Mapper.GraduateMapper;
import com.bnu.graduate.Service.GraduateService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;


@Service
@Transactional
public class GraduateServiceImpl extends ServiceImpl<GraduateMapper, Graduate> implements GraduateService {

    @Resource
    GraduateService graduateService;

    @Override
    public Object findAllByGrade(HttpServletRequest request) {
        //获取前台发送过来的数据
        Integer pageNo = Integer.valueOf(request.getParameter("pageNo"));
        Integer pageSize = Integer.valueOf(request.getParameter("pageSize"));
        String grade = String.valueOf(request.getParameter("grade"));
        //使用分页插件
        IPage<Graduate> page = new Page<>(pageNo, pageSize);
        //使用条件构造器
        QueryWrapper<Graduate> wrapper = new QueryWrapper<>();
        //添加条件
        wrapper.eq(StringUtils.isNotBlank(grade),"grade",grade)
                .orderByAsc("sId");
        //返回service层中的page()，向其中传入页面信息和查询条件
        return graduateService.page(page,wrapper);
    }

    @Override
    public Object findGraduateByWrapper(HttpServletRequest request) {
        Integer pageNo = Integer.valueOf(request.getParameter("pageNo"));
        Integer pageSize = Integer.valueOf(request.getParameter("pageSize"));
        String grade = String.valueOf(request.getParameter("grade"));
        String pa = String.valueOf(request.getParameter("pa"));
        String major = String.valueOf(request.getParameter("major"));
        String destinationType = String.valueOf(request.getParameter("destinationType"));
        String location = String.valueOf(request.getParameter("location"));

        IPage<Graduate> page = new Page<>(pageNo, pageSize);
        QueryWrapper<Graduate> wrapper = new QueryWrapper<>();
        wrapper.eq(StringUtils.isNotBlank(grade),"grade",grade)
                .eq(StringUtils.isNotBlank(major),"major",major)
                .eq(StringUtils.isNotBlank(destinationType),"destinationType",destinationType)
                .eq(StringUtils.isNotBlank(location),"location",location)
                .orderByAsc("sId");
        //pa是输入框接受的值，可以用来配合选择框进行搜索，也可以不填写，pa会从"name"、"destination"、"location"、"sid"中进行查询
        if (StringUtils.isNotBlank(pa)){
            wrapper.and(i -> i.like("name", pa).or().eq("destination", pa).or().eq("location", pa).or().eq("sId", pa));
        }
        return graduateService.page(page,wrapper);
    }
}
